/**
 * 第1阶段 模块1 题2
 */

package stage1.module1;

import java.util.Scanner;

public class Q2 {
    public static void main(String[] args) {

        // use scanner to get user input
        Scanner s =  new Scanner(System.in);

        System.out.println("Enter the size of the array:");
        // get the init array size
        int size = s.nextInt();

        // the next available position in the array
        int curr = 0;

        // data array storing user inputs
        String[] data = new String[size];
        String inp;

        while (true) {
            System.out.println("Enter:");
            inp = s.next();

            // stop the program when appropriate cmd is provided
            if (inp.equals("stop")) {
                break;
            }
            // store data in the array
            data[curr] = inp;
            // inc the pointer
            curr++;

            // expansion condition checking
            if ((double) curr / size >= 0.8) {
                System.out.println("80% of the space used, start expanding...");
                // new size
                size *= 1.5;

                // create a new array with the new size
                String[] temp = new String[(int) size];

                // move data to the new array
                System.out.println("migrating data...");
                for (int i = 0; i < data.length; i++) {
                    temp[i] = data[i];
                }
                data = temp;

                System.out.println("finished migrating.");
                System.out.println("finished expanding.");
            }
        }
        System.out.println("Program terminated.");
    }
}
